package org.test.client.ui.left;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.test.client.ui.MainPanel;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

/**
 * BoxLayout 暂时不知道如何控制组件的大小 
 * 故暂时使用flowlayout
 * @author Guodeqi
 *
 */
public class LeftMenuBar extends JPanel{
	public static boolean closedChildItem = true;
	public  static  Color labelColor = Icons.getColor("949494");
	public Font labelFont = new Font(Font.DIALOG,Font.BOLD,12); 
	public static int labelw = 230;
	public static int labelh = 25;
	
	private LeftMenuBar thisMBar = this;
	private MainPanel mainPanel = null;
	
	public static int menuBarWidth = 250; 
	public static int defaulth = 40;
	
	private int vgap = 0;
	
//	private BoxLayout layout = null;
	
	private Vector<JComponent> items = new Vector<JComponent>();
	
	private Vector<LeftMenuItem> singleSelGroup = new Vector<LeftMenuItem>();
	
	public LeftMenuBar(MainPanel panel){
		this.mainPanel = panel;
	}
	
	public LeftMenuBar doInit(){
		
//		layout = new BoxLayout(thisMBar, BoxLayout.Y_AXIS);
//		this.setLayout(layout);
		Component vph = Box.createVerticalStrut(menuBarWidth);
		vph.setPreferredSize(new Dimension(menuBarWidth,15));
		this.add(vph);
		int size = items.size();
		for(int i=0;i<size;i++){
			this.add(items.get(i));
		}
		
		this.setOpaque(false);
		this.setLayout(new FlowLayout(FlowLayout.LEFT,10,0));
		int height = getHeightForAllItem();
		this.setPreferredSize(new Dimension(menuBarWidth,height));
		return this;
	}
	
	public LeftMenuBar addItem(LeftMenuItem item){
		items.add(item);
		singleSelGroup.add(item);
		item.setMenuBar(thisMBar);
		return this;
	}
	
	public LeftMenuBar addItem(leftMenu itemMenu){
		items.add(itemMenu);
		itemMenu.setMenuBar(thisMBar);
		return this;
	}
	
	@Override
	public void paintComponent(Graphics g) {
//		super.paintComponents(g);
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		this.setBackground(Icons.getColor("ffffff"));
		int www = this.getWidth();
//		int hhh =this.getHeight();
		int hhh = getHeightForAllItem() + 50;
		
//		NinePatch popupBg = PopupIconFactory.getInstance().getPopupBg();
//		popupBg.draw(g2d, 0, 0, www, hhh);
		
	}
	
	public int getHeightForAllItem(){
		int size = items.size();
		int returnValue = 0;
		
		for(int i=0;i<size;i++){
			JComponent tmp = items.get(i);
			Dimension d = tmp.getPreferredSize();
			int h = tmp.getHeight() > d.height ? tmp.getHeight() : d.height;
			if(tmp instanceof LeftMenuItem){
				LeftMenuItem tmpItem = (LeftMenuItem)tmp;
				returnValue += h;
			}else if(tmp instanceof leftMenu){
				returnValue += h;
			}else if(tmp instanceof JLabel){
				returnValue += h;
			}
		}
		
		return returnValue + 30;
	}
	
	public void addLabel(String lableInfo){
		JLabel tmpLabel = new JLabel();
		tmpLabel.setHorizontalAlignment(10);
		tmpLabel.setForeground(labelColor);
		tmpLabel.setBackground(Color.black);
		tmpLabel.setFont(labelFont);
		tmpLabel.setText("    "+lableInfo);
		tmpLabel.setPreferredSize(new Dimension(labelw,labelh));
		items.add(tmpLabel);
	}
	
	public void setSelectOnItem(LeftMenuItem item){
		int size = singleSelGroup.size();
		for(LeftMenuItem tmp :singleSelGroup){
			if(tmp!=item && tmp.getPressed()){
				tmp.setNormal();
			}
		}
	}
	
	public void setSelectedItem(LeftMenuItem item){
		int size = singleSelGroup.size();
		for(LeftMenuItem tmp :singleSelGroup){
			if(tmp!=item && tmp.getPressed()){
				tmp.setNormal();
			}
		}
		item.setRollovered(true);
		item.setPressedForClick(true);
	}
	
}
